﻿; (function ($) {
    var methods = {
        init: function (options) {
            var setting = {
                modal: "",
                format: "yyyy-mm-dd",
                confirm: null,
                clear: null,
                min: "",
                max: ""
            };
            setting = $.extend(setting, options);
            var elem = $(this);
            var win_width = $(window).width();
            var controls, model = "scroller", display = "bottom";
            if (setting.modal == "time") {
                controls = ['time'];
            } else if (setting.modal == "date") {
                controls = ['calendar', 'time'];
            } else if (setting.modal == "calendar") {
                controls = ['calendar'];
            }
            if (win_width >= 768) {
                display = "modal";
                model = "mixed";
            }

            var lang = "zh";
            if (window.LangType != undefined) {
                if (window.LangType == "CN") {
                    lang = "zh";
                } else {
                    lang = "en";
                }
            }

            if (!setting.min && setting.modal != "time")
            {
                setting.min = new Date(1900, 1, 1);
            }

            if (!setting.max && setting.modal != "time") {
                setting.max = new Date(2099, 12, 31);
            }

            var calendarInstance = $(this).mobiscroll().calendar({
                theme: "bootstrap",
                lang: lang,
                display: display,
                mode: model,
                controls: controls,
                timeWheels: "HHii",
                dateFormat: setting.format,
                timeFormat: "HH:ii",
                rows: 6,
                minDate: setting.min,
                maxDate: setting.max,
                onSelect: function (v, inst) {
                    inst.IsClear = false;
                    inst.IsOpen = false;
                    if ($.isFunction(setting.confirm)) {
                        setting.confirm(v, $(this));
                    }
                    var calendarSelector = $(this);
                    if (calendarSelector.attr("confirm") != "" && calendarSelector.attr("confirm") != undefined) {
                        var SelectorConfirm = null;
                        eval("SelectorConfirm=" + calendarSelector.attr("confirm"));
                        if ($.isFunction(SelectorConfirm)) {
                            SelectorConfirm(v, calendarSelector);
                        }
                    }
                },
                onBeforeShow: function (inst) {
                    if ($(this).attr("disabled")) {
                        return false;
                    }
                },
                onShow: function (event, valueText, inst) {
                    if (win_width >= 768) {
                        $($("div.dwwol")[0]).css({ "display": "block", "margin-left": "-25px", "width": "200px" });
                    }
                    inst.$this = $(this);

                }, onCancel: function (event, inst) {
                    if (inst.IsClear) {
                        setTimeout(wmpDateClearValue($(this)), "300");
                        var calendarSelector = $(this);
                        if (calendarSelector.attr("clear") != "") {
                            var SelectorClear = null;
                            eval("SelectorClear=" + calendarSelector.attr("clear"));
                            if ($.isFunction(SelectorClear)) {
                                SelectorClear(calendarSelector);
                            }
                        }
                        if (calendarSelector.attr("confirm") != "" && calendarSelector.attr("confirm") != undefined) {
                            var SelectorConfirm = null;
                            eval("SelectorConfirm=" + calendarSelector.attr("confirm"));
                            if ($.isFunction(SelectorConfirm)) {
                                SelectorConfirm("", calendarSelector);
                            }
                        }
                    }
                    inst.IsOpen = false;
                },
                onSetDate: function (event, inst) {
                    if (setting.modal == "time") { return;}
                    if (inst.IsOpen == false || inst.IsOpen == undefined) {
                        /*当前选择值*/
                        var formatArray;
                        if (setting.format.indexOf('.') > -1) {
                            formatArray = setting.format.split('.');
                        } else if (setting.format.indexOf('/') > -1) {
                            formatArray = setting.format.split('/');
                        } else if (setting.format.indexOf('-') > -1) {
                            formatArray = setting.format.split('-');
                        }
                        var yearIndex = monthIndex = dayIndex = 0;
                        if (window.LangType == "EN") {
                            yearIndex = 2;
                            monthIndex = 0;
                            dayIndex = 1;
                        } else {
                            yearIndex = 0;
                            monthIndex = 1;
                            dayIndex = 2;
                        }

                        var month = parseInt(inst._wheelArray[monthIndex]) + 1;
                        var day = parseInt(inst._wheelArray[dayIndex]);

                        var value = inst._wheelArray[yearIndex] + "-" + (month < 10 ? "0" + month : month).toString() + "-" + (day < 10 ? "0" + day : day).toString();
                        var valdate = new Date(value);
                        if ($(this).val().trim() == "") {
                            valdate = new Date();
                        }
                        /*临时选择值*/

                        var _tempMonth = parseInt(inst._tempWheelArray[monthIndex]) + 1;
                        var _tempDay = parseInt(inst._tempWheelArray[dayIndex]);

                        var temp = inst._tempWheelArray[yearIndex] + "-" + (_tempMonth < 10 ? "0" + _tempMonth : _tempMonth).toString() + "-" + (_tempDay < 10 ? "0" + _tempDay : _tempDay).toString();
                        var valuedate = new Date(temp);

                        if (window.LangType != "EN")
                        {
                            if ($(this).val().trim() != "" && (valdate.getFullYear() != new Date($(this).val().trim().replace(/-/g, "/")).getFullYear()))
                            {
                                valdate = new Date(new Date($(this).val().trim()).getFullYear() + "-" + (month < 10 ? "0" + month : month).toString() + "-" + (day < 10 ? "0" + day : day).toString());
                            }
                        }

                        if (valuedate != valdate) {
                            if (inst.settings.lang == "zh") {
                                inst._tempWheelArray[0]= valdate.getFullYear();
                                inst._tempWheelArray[1]= valdate.getMonth();
                                inst._tempWheelArray[2]= valdate.getDate();
                                inst._wheelArray[0]= valdate.getFullYear();
                                inst._wheelArray[1]= valdate.getMonth();
                                inst._wheelArray[2]= valdate.getDate();
                            } else {
                                inst._tempWheelArray[2]= valdate.getFullYear();
                                inst._tempWheelArray[0]= valdate.getMonth();
                                inst._tempWheelArray[1]= valdate.getDate();
                                inst._wheelArray[2]= valdate.getFullYear();
                                inst._wheelArray[0]= valdate.getMonth();
                                inst._wheelArray[1]= valdate.getDate();
                        }
                            inst._tempvalue = value;
                            inst._value = value;
                            valuedate = valdate;
                    }
                        if($(this).attr("mindate") != "" && $(this).attr("mindate") != undefined && $(this).attr("mindate") != "Invalid Date") {
                            inst.settings.minDate = new Date($(this).attr("mindate"));
                            if (inst.settings.minDate == NaN || inst.settings.minDate.toString() == "Invalid Date") {
                                var mindate;
                                eval("mindate=" +$(this).attr("mindate"));
                                if (mindate != "" && mindate != undefined)
                                    inst.settings.minDate = new Date(mindate);
                                else
                                    inst.settings.minDate = NaN;
                        }
                            if (inst.settings.minDate != NaN)
                                if (valuedate < inst.settings.minDate) {
                                    if (inst.settings.lang == "zh") {
                                        inst._tempWheelArray[0]= inst.settings.minDate.getFullYear();
                                        inst._tempWheelArray[1]= inst.settings.minDate.getMonth();
                                        inst._tempWheelArray[2]= inst.settings.minDate.getDate();
                                    } else {
                                        inst._tempWheelArray[2]= inst.settings.minDate.getFullYear();
                                        inst._tempWheelArray[0]= inst.settings.minDate.getMonth();
                                        inst._tempWheelArray[1]= inst.settings.minDate.getDate();
                                }
                        }
                    }
                        if($(this).attr("maxdate") != "" && $(this).attr("maxdate") != undefined && $(this).attr("maxdate") != "Invalid Date") {
                            inst.settings.maxDate = new Date($(this).attr("maxdate"));
                            if (inst.settings.maxDate == NaN || inst.settings.maxDate.toString() == "Invalid Date") {
                                var maxDate;
                                eval("maxDate=" +$(this).attr("maxdate"));
                                if (maxDate != "" && maxDate != undefined)
                                    inst.settings.maxDate = new Date(maxDate);
                                else
                                    inst.settings.maxDate = NaN;

                        }
                            if (inst.settings.maxDate != NaN)
                                if (valuedate > inst.settings.maxDate) {
                                    if (inst.settings.lang == "zh") {
                                        inst._tempWheelArray[0]= inst.settings.maxDate.getFullYear();
                                        inst._tempWheelArray[1]= inst.settings.maxDate.getMonth();
                                        inst._tempWheelArray[2]= inst.settings.maxDate.getDate();
                                    } else {
                                        inst._tempWheelArray[2]= inst.settings.maxDate.getFullYear();
                                        inst._tempWheelArray[0]= inst.settings.maxDate.getMonth();
                                        inst._tempWheelArray[1]= inst.settings.maxDate.getDate();
                                }
                                    //inst.settings._tempValue = inst.settings.minDate;
                        }
                    }
                        inst.IsOpen = true;
                    }
                },
                buttons: [
                    {
                        text: window.Lang['text_wmp_confirm'],//'确认'
                        //handler: 'set',
                        cssClass: 'dwb btn btn-primary',
                        handler: function (e, inst) {
                            inst.handlers.set();
                            setTimeout(function () {
                                elem.blur();
                            }, 300)
                            
                        }
                    },
                    {
                        text: window.Lang['text_wmp_clear'],//'清除',
                        handler: function (e, inst) {
                            inst.IsClear = true;
                            if ($.isFunction(setting.clear)) {
                                setting.clear($(this));
                            }

                            inst.handlers.cancel();
                            setTimeout(function () {
                                elem.blur();
                            }, 300)
                        },
                        cssClass: 'dwb btn btn-default'
                    }
                ]
            });
            return calendarInstance;
        },
        time: function (options) {
            var setting = {
                modal: "time",
                confirm: null
            };
            setting = $.extend(setting, options);
            return this.wmpDate("init", setting);
        },
        date: function (options) {
            var setting = {
                modal: "date",
                confirm: null
            };
            setting = $.extend(setting, options);
            return this.wmpDate("init", setting);
        },
        calendar: function (options) {
            var setting = {
                modal: "calendar",
                confirm: null
            };
            setting = $.extend(setting, options);
            return this.wmpDate("init", setting);
        }
    };
    $.fn.wmpDate = function (method) {
        if (window != window.parent && typeof (window.parent.$("#").wmpDate) == "function") {
            if (arguments.length > 1) {

                return window.parent.$(this).wmpDate(method, arguments[1]);
            }
            else {
                return window.parent.$(this).wmpDate(method);
            }
        } else {
            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method' + method + 'does not exist');
            }
        }
    };
})($);
function wmpDateClearValue($this) {
    $this.val("");
}